package org.madmac.controller.usuario;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.madmac.modulo.EModulo;
import org.madmac.pessoa.Pessoa;
import org.madmac.servlet.action.Action;
import org.madmac.usuario.Usuario;
import org.madmac.usuario.UsuarioBo;
import org.madmac.usuario.UsuarioDao;

/**
 *
 * @author atila camurca
 */
public class UsuarioServlet extends HttpServlet {
   
   private static final long serialVersionUID = 1L;
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* 
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet UsuarioServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet UsuarioServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //processRequest(request, response);
       // TODO: ao cadastrar um usuario cadastrar tbm a pessoa.
       try {
          // se id for nulo, create.
          if (request.getParameter("id") == null) {
             // 1. criar pessoa e retornar id para chave estrangeira de usuario.
             // TODO: por a inserção de pessoa e usuario em transação.
             Pessoa pessoa = new Pessoa();
             pessoa.setNome(request.getParameter("nome"));
             
             Usuario usuario = new Usuario();
             usuario.setLogin(request.getParameter("login"));
             // TODO: validar a senha com javascript no cliente.
             usuario.setSenha(request.getParameter("senha"));
             usuario.setNickname(request.getParameter("nickname"));
             usuario.setModulo(EModulo.valueOf(request.getParameter("permissao")));

             Integer id = UsuarioBo.getInstance().validateAndCreate(usuario, pessoa);
             Usuario u = UsuarioDao.getInstance().findById(id);
             request.setAttribute("usuario", u);
             request.setAttribute("action", Action.LIST);   
          }
       } catch (Exception e) {
          request.setAttribute("error", e);
          request.setAttribute("action", Action.ERROR);
       } finally {
          RequestDispatcher dispatcher = request.getRequestDispatcher("usuario/usuario.jsp");
          dispatcher.forward(request, response);
       }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
